package message

import (
	"context"

	"api/common"
	"api/internal/svc"
	"api/internal/types"
	"api/pb/message"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetMessageCountLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetMessageCountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMessageCountLogic {
	return &GetMessageCountLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// GetMessageCount 获取消息数量
func (l *GetMessageCountLogic) GetMessageCount(req *types.MessageCountRequest) (resp *types.MessageCountResponse, err error) {
	/*
		1. 可以获取指定单聊人的消息数量(未读/已读)
		2. 可以获取指定群聊的消息数量(未读/已读)
		3. 可以获取所有消息数量(未读/已读)
	*/

	// 1. 获取用户标识
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	// 2. 获取消息数量
	result, err := l.svcCtx.MessageRPC.GetMessageCount(l.ctx, &message.MessageCountRequest{
		SenderId: req.SenderID, UserId: userID, Status: req.Status,
		ReceiverType: req.ReceiverType, ReceiverId: req.ReceiverID,
	})
	if err != nil {
		return nil, err
	}
	// 3. 构建响应
	resp = &types.MessageCountResponse{
		Total:     result.Total,
		GroupMap:  result.GroupMap,
		SingleMap: result.SingleMap,
	}
	return resp, nil
}
